package com.ganbo.user.client.service.impl;

import com.ganbo.user.api.api.UserClientApi;
import com.ganbo.user.api.model.User;
import com.ganbo.user.client.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserClientApi userClientApi;


//    @HystrixCommand(fallbackMethod = "getDefaultUser",
//            commandKey = "getUserCommon",
//            groupKey = "getUserGroup",
//            threadPoolKey = "getUserThreadPool",
//            ignoreExceptions = {NullPointerException.class})
    @Override
    public User getUser(Long id, String username) {
//        if (id == 1) {
//            throw new NullPointerException();
//        } else if (id == 2) {
//            throw new RuntimeException("这个异常不会被Histryx忽略,会出发熔断机制");
//        }
        return userClientApi.getUser(id, username);
    }

    @Override
    public User add(User user) {
        return userClientApi.add(user);
    }


    public User getDefaultUser(Long id, String username) {
        log.error("触发服务熔断,id:{},username:{}", id, username);
        return User.builder().id(-1L).username("默认值").createTime(new Date()).build();
    }
}
